
// 渲染数据
$(function () {
    initEvent();
});

function initEvent() {  
    var datas = {
        interface:'order@orderList',     
    };
    common.ajax({
        data: datas,
        success: function (data) {
            if (data.status == '2000') {
                if(data.result.length>0){
                    var arr = [];
                    $.each(data.result, function(i, o) {
                        var htmlstr = '';
                        htmlstr += '<li class="radius10 white-bg">';
                            htmlstr += '<div class="o-title">';
                                htmlstr += '<h2>'+o.partner_name+'</h2>';
                                htmlstr += '<p>'+o.p_name+'</p>';
                                htmlstr += '<span class="o-tip orange-color">'+o.status+'</span>';
                            htmlstr += '</div>';
                            htmlstr += '<div class="wrap4">';
                                htmlstr += '<div class="o-detail">';
                                    htmlstr += '<p class="clearfix">';
                                        htmlstr += '<span class="fl color6">学生姓名</span>';
                                        htmlstr += '<span class="fr">'+o.u_true_name+'</span>';
                                    htmlstr += '</p>';
                                    htmlstr += '<p class="clearfix">';
                                        htmlstr += '<span class="fl color6">课程金额</span>';
                                        htmlstr += '<span class="fr">¥'+o.b_borrow_amount+'</span>';
                                    htmlstr += '</p>';
                                    //判断模式是否是x+y模式
                                    if(o.p_return_type == 4){
                                        htmlstr += '<p class="clearfix">';
                                            htmlstr += '<span class="fl color6">前'+o.p_x_month+'月每月应交</span>';
                                            htmlstr += '<span class="fr">'+o.x_amount+'元</span>';
                                        htmlstr += '</p>';
                                        htmlstr += '<p class="clearfix">';
                                            htmlstr += '<span class="fl color6">后'+o.p_y_month+'月每月应交</span>';
                                            htmlstr += '<span class="fr">'+o.y_amount+'元</span>';
                                        htmlstr += '</p>';

                                    }else{
                                        htmlstr += '<p class="clearfix">';
                                            htmlstr += '<span class="fl color6">月交本金</span>';
                                            htmlstr += '<span class="fr">¥'+o.month_rent_amt+'</span>';
                                        htmlstr += '</p>';
                                    }
                                    htmlstr += '<p class="clearfix">';
                                        htmlstr += '<span class="fl color6">账单期数</span>';
                                        htmlstr += '<span class="fr">'+o.b_period+'期</span>';
                                    htmlstr += '</p>';
                                    htmlstr += '<p class="clearfix">';
                                        htmlstr += '<span class="fl color6">交费日</span>';
                                        htmlstr += '<span class="fr">每月'+o.repayment_day+'日</span>';
                                    htmlstr += '</p>';
                                htmlstr += '</div>';
                                if(o.b_status == 1001 || o.b_status == 2001 || o.b_status == 2002){
                                    htmlstr += '<div class="o-reason">';
                                        htmlstr += '<p>退回原因</p>';
                                        htmlstr += '<p class="color9">'+o.bt_remark+'</p>';
                                    htmlstr += '</div>';
                                }

                                htmlstr += '<div class="order-btn-par text-right">';
                                    if(o.b_status == '0000'){
                                        htmlstr += '<a href="javascript:void(0);" class="order-btn" onclick="orderCancel('+o.b_id+')">取消申请</a>';
                                        htmlstr += '<a href="/user/addmsg?id='+o.b_id+'" class="order-btn">继续申请</a>';
                                    }else if(o.b_status == '1000'){
                                        htmlstr += '<a class="order-btn" href="javascript:void(0);" onclick="orderCancel('+o.b_id+')">取消申请</a>';
                                        htmlstr += '<a class="order-btn" href="/order/jsubmitorder?id='+o.b_id+'">继续申请</a>';
                                    }else if(o.b_status == '1001'){
                                        if(o.partner_type == '1'){
                                            htmlstr += '<a class="order-btn" href="/order/jorderinfo?id='+o.b_id+'">账单详情</a>';
                                        }else{
                                            htmlstr += '<a class="order-btn" href="/order/orderinfo?id='+o.b_id+'">账单详情</a>';
                                        }
                                    }else if(o.b_status == '2001'){
                                        if(o.partner_type == '1'){
                                            htmlstr += '<a class="order-btn" href="/order/changeorder?id='+o.b_id+'">修改信息</a>';
                                        }else{
                                            htmlstr += '<a class="order-btn" href="/order/backorder?id='+o.b_id+'">修改信息</a>';
                                        }
                                    }else{
                                        if(o.b_status != '3201'){
                                            htmlstr += '<a class="order-btn" href="/order/spaymentplan?id='+o.b_id+'">交费计划</a>';
                                        }
                                        if(o.partner_type == '1'){
                                            htmlstr += '<a class="order-btn" href="/order/jorderinfo?id='+o.b_id+'">账单详情</a>';
                                        }else{
                                            htmlstr += '<a class="order-btn" href="/order/orderinfo?id='+o.b_id+'">账单详情</a>';
                                        }
                                    }
                                htmlstr += '</div>';
                            htmlstr += '</div>';
                        htmlstr += '</li>';
                        
                        arr.push(htmlstr);  
                    });
                    $('.o-list').html(arr.join(''));
                }else{
                    var html = '<div class="error wrap text-center">'
                        html += '<img class="apply" src="/static/images/apply.png" alt="">'
                        html += '<p class="color9">暂无账单</p>'
                        html += '<a class="goapply_btn orange-bg text-center" href="/user/mechanism" >立即申请</a>'
                    html += '</div>'
                    $('body').html(html);
                }
            } else if (data.status == '5000') {
                falert(data.msg);
            }
        }
    });  
}

//点击提交弹出遮罩层
function orderCancel(id) {
    layer.open({
        type: 1,
        title: false,
        closeBtn: 0,
        skin: 'yourclass',
        btn:['确认取消','暂不取消'],
        success:function(layero){
          layero.find('.layui-layer-btn').css('text-align', 'center');
        },
        shadeClose: false,
        area: ['82%'],
        content: '确认取消后，该订单将被变更为取消状态，您是否确认取消？',
        btn1: function(index, layero){
            var datas = {
                interface:'order@cancelOrder',       
            };
    	    if(id){
    	        datas.b_id = id;
    	    }
            common.ajax({
                data: datas,
                success: function (data) {
                    if (data.status == '2000') {
                        falert('订单取消成功','/order/orderlist');
                    } else if (data.status == '5000') {
                        falert(data.msg);
                    }
                }
            });
        }
    });
}